package com.controller;

import com.entity.Admin;
import org.apache.ibatis.executor.ResultExtractor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.entity.User;
import com.service.AdminService;

import com.entity.Result;

@SuppressWarnings({"all"})
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @PostMapping("/login")
    public Result login(Admin admin) {
        try {
            return Result.success(adminService.login(admin));
        } catch (Exception e) {
            return Result.error(adminService.login(admin));
        }

    }

    @GetMapping("/list")
    public Result getUserList() {
        try{
            return Result.success(adminService.getAllUser());
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @DeleteMapping("/del")
    public Result delectUser(User user) {
        try {
            return Result.success(adminService.deleteUser(user));
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/update")
    public Result updateUser(User user) {
        try {
            System.out.println("updateUser："+user);
            return Result.success(adminService.upDateUser(user));
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }
}
